<?php

namespace app\controller\api;

use app\base\BaseEncryptController;
use app\model\business\BusinessModel;
use app\model\user\UserCartModel;
use app\model\user\UserModel;
use think\Request;

/**
 * User
 * Class UserCart
 * @package app\controller
 */
class UserCart extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new UserModel();
    }

    /**
     * 加入购物车
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onAddCart(Request $request)
    {
        $goods_id = $request->param("goods_id");
        $attribute_ids = $request->param("attribute_ids");
        $goods_number = $request->param("goods_number");
        $type = $request->param("type");
        $userInfo = $this->currentUser;//登录信息
        if (!isset($userInfo['user_id'])) {
            $res = [
                'code' => 0,
                'data' => []
            ];
            return self::returnMsg($res);
        }
        $user_car_model = new UserCartModel();
        $res = $user_car_model->addCart($userInfo['user_id'], $goods_id, $attribute_ids, $type, $goods_number);
        return self::returnMsg($res);
    }

    /**
     * 我的购物车
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onCartList(Request $request)
    {
        $goods_id = $request->param("goods_id");
        $userInfo = $this->currentUser;//登录信息
        if (!isset($userInfo['user_id'])) {
            $userInfo['user_id'] = 0;
        }
        $user_car_model = new UserCartModel();
        $member_level = UserModel::getIsMemberLevel($userInfo['user_id']);
        $car_list = $user_car_model->getListById($userInfo['user_id'], 0, true, $member_level, $goods_id);
        $res = [
            'code' => 1,
            'data' => $car_list
        ];
        return self::returnMsg($res);
    }

    /**
     * 修改我的购物车
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function onCartEdit(Request $request)
    {
        $id = $request->param("id");
        $goods_number = $request->param("goods_number");
        $type = $request->param("type");
        $userInfo = $this->currentUser;//登录信息
        $user_car_model = new UserCartModel();
        $res = $user_car_model->changeCart($id, $userInfo['user_id'], $goods_number, $type);
        return self::returnMsg($res);
    }

    /**
     * 删除我的购物车
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function onCartDelete(Request $request)
    {
        $id = $request->param("id");
        $type = $request->param("type");
        $userInfo = $this->currentUser;//登录信息
        $user_car_model = new UserCartModel();
        $res = $user_car_model->deleteCart($id, $userInfo['user_id'], $type);
        return self::returnMsg($res);
    }

    /**
     * 设置公益弹窗时用户的选择
     * @param Request $request
     * @return \think\response\Json
     */
    public function setPublicType(Request $request)
    {
        $public_type = $request->param("public_type");
        $userInfo = $this->currentUser;//登录信息
        if (empty($public_type)) {
            $rsps['code'] = 0;
            return self::returnMsg($rsps);
        }
        $user_model = new UserModel();
        $up_data = [
            'id' => $userInfo['user_id'],
            'public_type' => $public_type
        ];
        $res = $user_model->update($up_data);
        $rsps['code'] = $res ? 1 : 0;
        return self::returnMsg($rsps);
    }

}
